package com.yingpai.fitness.dvp_2_0.main.activity.shop;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.bumptech.glide.Glide;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.adapter.BaseAdapterHelper;
import com.yingpai.fitness.dvp_2_0.common.adapter.QuickAdapter;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.util.GsonUtil;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.common.view.XGridView;
import com.yingpai.fitness.dvp_2_0.main.bean.CommentListBean;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;
import com.yingpai.fitness.entity.ShopDetailMcipChildBean;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

/**
 * Created by ASUS on 2019/1/15.
 */

public class AllCommentActivity extends CommonActivity implements OnRefreshLoadMoreListener {

    private int merchandiseId;
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private int pageNum = 1;
    private int pageSize = 10;
    private QuickAdapter<CommentListBean.MapBean.PageInfoBean.ListBean> mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.NORMAL_STATUS);
        setContentView(R.layout.activity_dynamic_list);
    }

    @Override
    protected void initView() {
        refreshLayout.setOnRefreshLoadMoreListener(this);
        refreshLayout.setRefreshHeader(new MaterialHeader(context).setColorSchemeResources(R.color.common_color));
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            merchandiseId = bundle.getInt("merchandiseId", -1);
        }
        setTitle("全部评论");

        mAdapter = new QuickAdapter<CommentListBean.MapBean.PageInfoBean.ListBean>(this, R.layout.item_comment_shop) {
            @Override
            protected void convert(BaseAdapterHelper helper, CommentListBean.MapBean.PageInfoBean.ListBean item) {
                ImageView head_photo = helper.getView(R.id.head_photo);
                Glide.with(context)
                        .load(item.getCustomerPhotoUrl())
                        .centerCrop()
                        .error(R.mipmap.icon_user_default)
                        .into(head_photo);
                helper.setText(R.id.name, item.getCustomerName());
                List<ShopDetailMcipChildBean> mcipList = GsonUtil.jsonToList(item.getMcipValue(), ShopDetailMcipChildBean.class);
                StringBuilder stringBuilder = new StringBuilder();
                for (int i = 0; i < mcipList.size(); i++) {
                    if (i == mcipList.size() - 1) {
                        stringBuilder.append(mcipList.get(i).getPropertyValue());
                    } else {
                        stringBuilder.append(mcipList.get(i).getPropertyValue() + ";");
                    }
                }
                helper.setText(R.id.time_specs, item.getCreateTime().substring(0, item.getCreateTime().indexOf(" ")) + "  " + stringBuilder.toString());
                helper.setText(R.id.content, item.getComment());

                if (item.getPhotoUrls() != null && !"".equals(item.getPhotoUrls())) {
                    XGridView gridView = helper.getView(R.id.gridview);
                    List<String> list = Arrays.asList(item.getPhotoUrls().split(","));
                    QuickAdapter<String> quickAdapter = new QuickAdapter<String>(context, R.layout.item_comment_img, list) {
                        @Override
                        protected void convert(BaseAdapterHelper helper, String item) {
                            ImageView imageView = helper.getView(R.id.imageview);
                            Glide.with(context)
                                    .load(item)
                                    .centerCrop()
                                    .placeholder(R.mipmap.default_small_bg)
                                    .into(imageView);
                        }
                    };

                    gridView.setAdapter(quickAdapter);
                }

            }
        };
        listview.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        requestData();
    }

    private void requestData() {
        showDialog(this, null);
        HttpParams params = new HttpParams();
        params.put("merchandiseId", merchandiseId);
        params.put("pageNum", pageNum++);
        params.put("pageSize", pageSize);
        finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.ALL_COMMENT_LIST, params, Url.ALL_COMMENT_LIST, new Callback<CommentListBean>() {
            @Override
            public void onSuccess(CommentListBean commentListBean) {
                if (Constant.REQUEST_SUCCESS.equals(commentListBean.getResult())) {
                    setTitle("全部评论(" + commentListBean.getMap().getPageInfo().getTotal() + ")");
                    if (pageNum - 1 == 1) {
                        if (commentListBean.getMap().getPageInfo().getTotal() > 0) {
                            empty.setVisibility(View.GONE);
                            mAdapter.clear();
                            mAdapter.addAll(commentListBean.getMap().getPageInfo().getList());
                        } else {
                            empty.setVisibility(View.VISIBLE);
                        }

                    } else {
                        mAdapter.addAll(commentListBean.getMap().getPageInfo().getList());
                    }

                    int pagecount;
                    if (commentListBean.getMap().getPageInfo().getTotal() % pageSize == 0) {
                        pagecount = commentListBean.getMap().getPageInfo().getTotal() / pageSize;
                    } else {
                        pagecount = commentListBean.getMap().getPageInfo().getTotal() / pageSize + 1;
                    }

                    if (pageNum > pagecount) {
                        refreshLayout.setNoMoreData(true);
                        refreshLayout.finishLoadMoreWithNoMoreData();
                    } else {
                        refreshLayout.setNoMoreData(false);
                    }

                    refreshLayout.finishRefresh();
                    refreshLayout.finishLoadMore();
                    mAdapter.notifyDataSetChanged();
                } else {
                    if (pageNum - 1 == 1) {
                        empty.setVisibility(View.VISIBLE);
                    } else {
                        empty.setVisibility(View.GONE);
                    }
                    showToast(commentListBean.getMsg());
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                dismissDialog();
                if (pageNum - 1 == 1) {
                    empty.setVisibility(View.VISIBLE);
                } else {
                    empty.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        requestData();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageNum = 1;
        requestData();
    }
}
